﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Xushre.WeiBoSDK {
    /// <summary>
    /// 微博客户端
    /// </summary>
    public partial class OClient2 {

        /// <summary>
        /// 根据微博ID返回某条微博的评论列表
        /// </summary>
        /// <param name="id">需要查询的微博ID。</param>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filter_by_author">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsShow(Int64 id, Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int filter_by_author = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("id", id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("filter_by_author", filter_by_author);

            try {
                response = Get(API.OClient2.Comments.SHOW, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户所发出的评论列表
        /// </summary>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filter_by_source">来源筛选类型，0：全部、1：来自微博的评论、2：来自微群的评论，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsByMe(Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int filter_by_source = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("filter_by_source", filter_by_source);

            try {
                response = Get(API.OClient2.Comments.BY_ME, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户所接收到的评论列表
        /// </summary>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filter_by_author">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <param name="filter_by_source">来源筛选类型，0：全部、1：来自微博的评论、2：来自微群的评论，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsToMe(Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int filter_by_author = 0, int filter_by_source = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("filter_by_author", filter_by_author);
            data.Add("filter_by_source", filter_by_source);

            try {
                response = Get(API.OClient2.Comments.TO_ME, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取当前登录用户的最新评论包括接收到的与发出的
        /// </summary>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="trim_user">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsTimeline(Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int trim_user = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("trim_user", trim_user);

            try {
                response = Get(API.OClient2.Comments.TIMELINE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 获取最新的提到当前登录用户的评论，即@我的评论
        /// </summary>
        /// <param name="since_id">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="max_id">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filter_by_author">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <param name="filter_by_source">来源筛选类型，0：全部、1：来自微博的评论、2：来自微群的评论，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsMentions(Int64 since_id = 0, Int64 max_id = 0, int count = 50, int page = 1, int filter_by_author = 0, int filter_by_source = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("since_id", since_id);
            data.Add("max_id", max_id);
            data.Add("count", count);
            data.Add("page", page);
            data.Add("filter_by_author", filter_by_author);
            data.Add("filter_by_source", filter_by_source);

            try {
                response = Get(API.OClient2.Comments.MENTIONS, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 根据评论ID批量返回评论信息
        /// </summary>
        /// <param name="cid">需要查询的批量评论ID，最大50。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsShowBatch(Int64[] cid) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (cid.Length < 0 || cid.Length > 50) {
                return "{\"error\":\"Length of the CID parameter must be greater than 0 and less than 50.\",\"error_code\":9012}";
            }

            string cids = string.Join(",", cid);

            data.Add("cids", cids);

            try {
                response = Get(API.OClient2.Comments.SHOW_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }


        /// <summary>
        /// 对一条微博进行评论
        /// </summary>
        /// <param name="comment">评论内容，必须做URLencode，内容不超过140个汉字。</param>
        /// <param name="id">需要评论的微博ID。</param>
        /// <param name="comment_ori">当评论转发微博时，是否评论给原微博，0：否、1：是，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsCreate(string comment, Int64 id, int comment_ori = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (Encoding.Default.GetByteCount(comment) > 280) {
                comment = Tools.isSingle(comment) ? comment.Substring(0, 140) : comment.Substring(0, 280);
            }

            data.Add("comment", comment);
            data.Add("id", id);
            data.Add("comment_ori", comment_ori);

            try {
                response = Post(API.OClient2.Comments.CREATE, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 删除一条评论
        /// </summary>
        /// <param name="cid">要删除的评论ID，只能删除登录用户自己发布的评论。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsDestory(Int64 cid) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            data.Add("cid", cid);

            try {
                response = Post(API.OClient2.Comments.DESTORY, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 根据评论ID批量删除评论
        /// </summary>
        /// <param name="cids">需要删除的评论ID，用半角逗号隔开，最多20个。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsDestoryBatch(Int64[] cids) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (cids.Length < 1 || cids.Length > 20) {
                return "{\"error\":\"Length of the CIDS parameter must be greater than 0 and less than 20.\",\"error_code\":9013}";
            }

            string cid = String.Join(",", cids);

            data.Add("cid", cid);

            try {
                response = Post(API.OClient2.Comments.DESTORY_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }

        /// <summary>
        /// 回复一条评论
        /// </summary>
        /// <param name="cid">需要回复的评论ID。</param>
        /// <param name="id">需要评论的微博ID。</param>
        /// <param name="comment">回复评论内容，必须做URLencode，内容不超过140个汉字。</param>
        /// <param name="without_mention">回复中是否自动加入“回复@用户名”，0：是、1：否，默认为0。</param>
        /// <param name="comment_ori">当评论转发微博时，是否评论给原微博，0：否、1：是，默认为0。</param>
        /// <returns>JSON 格式的客串</returns>
        /// <exception cref="WeiboException"></exception>
        public string CommentsReply(Int64 cid, Int64 id, string comment, int without_mention = 0, int comment_ori = 0) {
            string response = string.Empty;
            Dictionary<string, object> data = new Dictionary<string, object>();

            if (Encoding.Default.GetByteCount(comment) > 280) {
                comment = Tools.isSingle(comment) ? comment.Substring(0, 140) : comment.Substring(0, 280);
            }

            data.Add("cid", cid);
            data.Add("id", id);
            data.Add("comment", comment);
            data.Add("without_mention", without_mention);
            data.Add("comment_ori", comment_ori);

            try {
                response = Post(API.OClient2.Comments.DESTORY_BATCH, data);
            } catch (WeiboException e) {
                throw e;
            }

            return response;
        }
    }
}
